package com.ruoyi.business.service.impl;

import com.ruoyi.business.constant.IBusinessConstant;
import com.ruoyi.business.domain.*;
import com.ruoyi.business.mapper.TenantComponentMapper;
import com.ruoyi.business.service.ITenantComponentParametersVersionService;
import com.ruoyi.business.service.ITenantComponentReturnValueVersionService;
import com.ruoyi.business.service.ITenantComponentService;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.translation.service.TranslationService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 租户级复合组件Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-18
 */
@Service
public class TenantComponentServiceImpl implements ITenantComponentService
{
    @Autowired
    private TenantComponentMapper tenantComponentMapper;
    @Autowired
    private ITenantComponentParametersVersionService tenantComponentParametersVersionService;
    @Autowired
    private ITenantComponentReturnValueVersionService tenantComponentReturnVersionService;
    @Autowired
    private TranslationService translationService;
    /**
     * 查询租户级复合组件
     *
     * @param id 租户级复合组件主键
     * @return 租户级复合组件
     */
    @Override
    public TenantComponent selectTenantComponentById(Long id)
    {
        return tenantComponentMapper.selectTenantComponentById(id);
    }

    /**
     * 查询租户级复合组件列表
     *
     * @param tenantComponent 租户级复合组件
     * @return 租户级复合组件
     */
    @Override
    public List<TenantComponent> selectTenantComponentList(TenantComponent tenantComponent)
    {
        return tenantComponentMapper.selectTenantComponentList(tenantComponent);
    }

    /**
     * 新增租户级复合组件
     *
     * @param tenantComponent 租户级复合组件
     * @return 结果
     */
    @Override
    public int insertTenantComponent(TenantComponent tenantComponent)
    {
        tenantComponent.setCreateTime(DateUtils.getNowDate());
        return tenantComponentMapper.insertTenantComponent(tenantComponent);
    }

    /**
     * 修改租户级复合组件
     *
     * @param tenantComponent 租户级复合组件
     * @return 结果
     */
    @Override
    public int updateTenantComponent(TenantComponent tenantComponent)
    {
        tenantComponent.setUpdateTime(DateUtils.getNowDate());
        return tenantComponentMapper.updateTenantComponent(tenantComponent);
    }

    /**
     * 批量删除租户级复合组件
     *
     * @param ids 需要删除的租户级复合组件主键
     * @return 结果
     */
    @Override
    public int deleteTenantComponentByIds(Long[] ids)
    {
        return tenantComponentMapper.deleteTenantComponentByIds(ids);
    }

    /**
     * 删除租户级复合组件信息
     *
     * @param id 租户级复合组件主键
     * @return 结果
     */
    @Override
    public int deleteTenantComponentById(Long id)
    {
        return tenantComponentMapper.deleteTenantComponentById(id);
    }

    @Override
    public int commitAdd(TenantComponentComposite tenantComponentComposite) {
        return 0;
    }

    @Override
    public int commitUpdate(TenantComponentComposite tenantComponentComposite) {
        return 0;
    }

    @Override
    public Long insertDraftComposite(TenantComponentComposite tenantComponentComposite) {
        fillInsertDefaultValues(tenantComponentComposite);
        //主表状态设置为草稿
        tenantComponentComposite.getTenantComponent().setStatus(IBusinessConstant.STATUS_DRAF);
        //插入基本信息
        tenantComponentComposite.getTenantComponent().setIsCurrent("N");
        tenantComponentMapper.insertTenantComponent(tenantComponentComposite.getTenantComponent());
        //插入复合组件参数信息
        for(TenantComponentParametersVersion tenantComponentParametersVersion:tenantComponentComposite.getTenantComponentParametersVersions()){
            tenantComponentParametersVersionService.insertTenantComponentParametersVersion(tenantComponentParametersVersion);
        }
        //插入复合组件返回值信息
        for(TenantComponentReturnValueVersion tenantComponentReturnValueVersion:tenantComponentComposite.getTenantComponentReturnValueVersions()){
            tenantComponentReturnVersionService.insertTenantComponentReturnValueVersion(tenantComponentReturnValueVersion);
        }
        return tenantComponentComposite.getTenantComponent().getComponentId();
    }

    private void fillInsertDefaultValues(TenantComponentComposite tenantComponentComposite) {
        //基本信息
        tenantComponentComposite.getTenantComponent().setVersion(1);
        tenantComponentComposite.getTenantComponent().setEnterpriseCode(SecurityContextHolder.getEnterpriseCode());
        //设置状态为已提交
        tenantComponentComposite.getTenantComponent().setStatus(IBusinessConstant.STATUS_SUBMIT);
        tenantComponentComposite.getTenantComponent().setCreateBy(SecurityContextHolder.getUserName());
        tenantComponentComposite.getTenantComponent().setCreateTime(DateUtils.getNowDate());
        tenantComponentComposite.getTenantComponent().setComponentKey(translationService.generateComponentKey(tenantComponentComposite.getTenantComponent().getFunctionDesc()));
//        //版本表信息
//        tenantComponentComposite.getTenantFunctionVersion().setVersion(1);
//        tenantComponentComposite.getTenantFunctionVersion().setIsCurrent("Y");
//        tenantComponentComposite.getTenantFunctionVersion().setEnterpriseCode(SecurityContextHolder.getEnterpriseCode());
//        tenantComponentComposite.getTenantFunctionVersion().setCreateBy(SecurityContextHolder.getUserName());
//        tenantComponentComposite.getTenantFunctionVersion().setCreateTime(DateUtils.getNowDate());
//        tenantComponentComposite.getTenantFunctionVersion().setFunctionCode(tenantComponentComposite.getTenantComponent().getFunctionCode());
//        tenantComponentComposite.getTenantFunctionVersion().setClassName(tenantComponentComposite.getTenantComponent().getClassName());
//        //参数表信息
//        for(TenantFunctionParametersVersion tenantFunctionParametersVersion:tenantComponentComposite.getTenantFunctionParametersVersions()){
//            tenantFunctionParametersVersion.setVersion(1);
//            tenantFunctionParametersVersion.setParameterId(null);
//            tenantFunctionParametersVersion.setEnterpriseCode(SecurityContextHolder.getEnterpriseCode());
//            tenantFunctionParametersVersion.setCreateBy(SecurityContextHolder.getUserName());
//            tenantFunctionParametersVersion.setCreateTime(DateUtils.getNowDate());
//            tenantFunctionParametersVersion.setFunctionCode(tenantComponentComposite.getTenantComponent().getFunctionCode());
//            tenantFunctionParametersVersion.setIsCurrent("Y");
//            tenantFunctionParametersVersion.setParameterCode(getNewParameterCode());
//            tenantFunctionParametersVersion.setClassName(tenantComponentComposite.getTenantComponent().getClassName());
//        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long updateDraftComposite(TenantComponentComposite tenantComponentComposite) {
        //判断当前主表状态
        //如果是草稿，则更新
        if(IBusinessConstant.STATUS_DRAF.equals(tenantComponentComposite.getTenantComponent().getStatus())){
            //简单更新即可
            tenantComponentMapper.updateTenantComponent(tenantComponentComposite.getTenantComponent());
            //删除原来参数，再插入参数
            tenantComponentParametersVersionService.deleteParametersVersionByComponentKey(
                    tenantComponentComposite.getTenantComponent().getComponentKey(),
                    tenantComponentComposite.getTenantComponent().getEnterpriseCode(),
                    tenantComponentComposite.getTenantComponent().getVersion()
            );
            for (TenantComponentParametersVersion tenantComponentParametersVersion : tenantComponentComposite.getTenantComponentParametersVersions()) {
                tenantComponentParametersVersionService.insertTenantComponentParametersVersion(tenantComponentParametersVersion);
            }
            //删除原来返回值列表，再插入返回值
            tenantComponentReturnVersionService.deleteReturnValueVersionByComponentKey(
                    tenantComponentComposite.getTenantComponent().getComponentKey(),
                    tenantComponentComposite.getTenantComponent().getEnterpriseCode(),
                    tenantComponentComposite.getTenantComponent().getVersion()
            );
            for (TenantComponentReturnValueVersion tenantComponentReturnValueVersion : tenantComponentComposite.getTenantComponentReturnValueVersions()) {
                tenantComponentReturnVersionService.insertTenantComponentReturnValueVersion(tenantComponentReturnValueVersion);
            }
            //如果是已提交，则然后更新当前主表记录为非当前版本,版本号加1,插入主表记录，然后插入功能点版本表及参数表
        }else{
            tenantComponentComposite.getTenantComponent().setIsCurrent(IBusinessConstant.SYSTEM_NO);
            //版本号+1
            fillUpdateDefaultValues(tenantComponentComposite);
            tenantComponentMapper.updateTenantComponent(tenantComponentComposite.getTenantComponent());
            tenantComponentComposite.getTenantComponent().setComponentId(0L);
            tenantComponentComposite.getTenantComponent().setIsCurrent(IBusinessConstant.SYSTEM_YES);
            //主表状态设置为草稿
            tenantComponentComposite.getTenantComponent().setStatus(IBusinessConstant.STATUS_DRAF);
            tenantComponentMapper.insertTenantComponent(tenantComponentComposite.getTenantComponent());
            tenantComponentComposite.getTenantComponent().setIsCurrent("N");
            //插入参数表
            for (TenantComponentParametersVersion tenantComponentParametersVersion : tenantComponentComposite.getTenantComponentParametersVersions()) {
                tenantComponentParametersVersionService.insertTenantComponentParametersVersion(tenantComponentParametersVersion);
            }
            //插入返回值表
            for (TenantComponentReturnValueVersion tenantComponentReturnValueVersion : tenantComponentComposite.getTenantComponentReturnValueVersions()) {
                tenantComponentReturnVersionService.insertTenantComponentReturnValueVersion(tenantComponentReturnValueVersion);
            }
        }
        return tenantComponentComposite.getTenantComponent().getComponentId();
    }

    private void fillUpdateDefaultValues(TenantComponentComposite tenantComponentComposite) {
        int newVersion = tenantComponentComposite.getTenantComponent().getVersion()+1;
        //设置状态为提交
        tenantComponentComposite.getTenantComponent().setStatus(IBusinessConstant.STATUS_SUBMIT);
        tenantComponentComposite.getTenantComponent().setVersion(newVersion);
        tenantComponentComposite.getTenantComponent().setIsCurrent(IBusinessConstant.SYSTEM_YES);
        tenantComponentComposite.getTenantComponent().setVersion(newVersion);
        if(StringUtils.isEmpty(tenantComponentComposite.getTenantComponent().getEnterpriseCode())){
            tenantComponentComposite.getTenantComponent().setEnterpriseCode(tenantComponentComposite.getTenantComponent().getEnterpriseCode());
        }
        if(StringUtils.isEmpty(tenantComponentComposite.getTenantComponent().getCreateBy())){
            tenantComponentComposite.getTenantComponent().setCreateBy(SecurityContextHolder.getUserName());
        }
        if(StringUtils.isEmpty(tenantComponentComposite.getTenantComponent().getUpdateBy())){
            tenantComponentComposite.getTenantComponent().setUpdateBy(SecurityContextHolder.getUserName());
        }
        tenantComponentComposite.getTenantComponent().setUpdateTime(DateUtils.getNowDate());
        tenantComponentComposite.getTenantComponent().setCreateTime(DateUtils.getNowDate());
        //更新参数列表
        for(TenantComponentParametersVersion tenantComponentParametersVersion:tenantComponentComposite.getTenantComponentParametersVersions()){
            tenantComponentParametersVersion.setVersion(newVersion);
            tenantComponentParametersVersion.setIsCurrent(IBusinessConstant.SYSTEM_YES);
            if(StringUtils.isEmpty(tenantComponentParametersVersion.getComponentKey())){
                tenantComponentParametersVersion.setComponentKey(tenantComponentComposite.getTenantComponent().getComponentKey());
            }
            if(StringUtils.isEmpty(tenantComponentParametersVersion.getCreateBy())){
                tenantComponentParametersVersion.setCreateBy(SecurityContextHolder.getUserName());
            }
            tenantComponentParametersVersion.setUpdateBy(SecurityContextHolder.getUserName());
            tenantComponentParametersVersion.setUpdateTime(DateUtils.getNowDate());
        }
        //更新返回值列表
        for(TenantComponentReturnValueVersion tenantComponentReturnValueVersion:tenantComponentComposite.getTenantComponentReturnValueVersions()){
            tenantComponentReturnValueVersion.setVersion(newVersion);
            tenantComponentReturnValueVersion.setIsCurrent(IBusinessConstant.SYSTEM_YES);
            if(StringUtils.isEmpty(tenantComponentReturnValueVersion.getComponentKey())){
                tenantComponentReturnValueVersion.setComponentKey(tenantComponentComposite.getTenantComponent().getComponentKey());
            }
            if(StringUtils.isEmpty(tenantComponentReturnValueVersion.getCreateBy())){
                tenantComponentReturnValueVersion.setCreateBy(SecurityContextHolder.getUserName());
            }
            tenantComponentReturnValueVersion.setUpdateBy(SecurityContextHolder.getUserName());
        }
    }
}
